<template>
    <div class="edit-popup">
        <popup
            ref="popupRef"
            :title="popupTitle"
            :async="true"
            width="550px"
            @confirm="handleSubmit"
            @close="handleClose"
        >
            <el-form
                class="ls-form"
                ref="formRef"
                :rules="rules"
                :model="formData"
            >
                <el-form-item label="名称" prop="title">
                    <el-input v-model="formData.title" placeholder="请输入名称" clearable />
                </el-form-item>
                <el-form-item label="描述" prop="description">
                    <el-input type="textarea" :row="4" v-model="formData.description" placeholder="请输入描述" clearable />
                </el-form-item>
                <el-form-item label="图片" prop="url">
                    <div>
                    <uploadimg v-model="formData.url"/>  
                    <span  style="color: #999;font-size: 12px;">
                        首页图片尺寸1400px，技术特色图片尺寸800px，高度一致即可
                    </span>
                    </div>
                </el-form-item> 
                <el-form-item label="首页跳转地址" prop="content">
                    <el-input v-model="formData.content" placeholder="请输入首页跳转地址" clearable />
                    <span style="color: #999;font-size: 12px;">
                        首页跳转地址，仅在位置为首页时有效
                    </span>
                </el-form-item>
                <el-form-item label="位置" required prop="type">
                    <el-radio-group v-model="formData.type">
                        <el-radio :label="1">工作台</el-radio>
                      <el-radio :label="2">关于我们</el-radio>
                        <el-radio :label="0">首页</el-radio>
                    </el-radio-group>
                </el-form-item>
              <el-form-item label="位置" required prop="type" v-if="formData.type===0 || formData.type===2">
                <el-radio-group v-model="formData.subType" v-if="formData.type===0">
                  <el-radio :label="2">下</el-radio>
                  <el-radio :label="1">上</el-radio>
                </el-radio-group>
                <el-radio-group v-model="formData.subType" v-if="formData.type===2">
                  <el-radio :label="1">卡片</el-radio>
                  <el-radio :label="2">资质</el-radio>
                </el-radio-group>
              </el-form-item>
            </el-form>
        </popup>
    </div>
</template>
<script   setup> 
import Popup from '@/components/popup/index.vue'
import { addBanner, editBanner, getBannerDetail } from '@/apis/banner'
const emit = defineEmits(['success', 'close'])
const formRef = shallowRef()
const popupRef = shallowRef()
const mode = ref('add')
const popupTitle = computed(() => {
    return mode.value == 'edit' ? '编辑轮播图' : '新增轮播图'
})
const formData = reactive({
    id: '',
    title: '',   
    url:'',
    description:'',
    type: 0,
    content:'',
    subType:1
})

const rules = {
    title: [
        {
            required: true,
            message: '请输入名称',
            trigger: ['blur']
        }
    ],
    image: [
        {
            required: true,
            message: '请上传图片',
            trigger: ['blur']
        }
    ]
}

const handleSubmit = async () => {
    await formRef.value?.validate()
    mode.value == 'edit' ? await editBanner(formData) : await addBanner({ ...formData, id: undefined })
    popupRef.value?.close()
    emit('success')
}

const handleClose = () => {
    emit('close')
}

const open = (type = 'add') => {
    mode.value = type
    popupRef.value?.open()
}

const setFormData = async (data) => {
        if (mode.value == 'edit') {
            const res = await getBannerDetail({
                id:data.id
            })
            for (const key in formData) {
            if (res[key] != null && res[key] != undefined) {
                //@ts-ignore
                formData[key] = res[key]
            }
        }
    } 
}

defineExpose({
    open,
    setFormData
})
</script>
